package com.uzai.console.service.cms.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.cms.personalcenter.CmsPersonalCenterConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.cms.CmsPersonalCenterService;
import com.uzai.console.vo.cms.personalcenter.CmsPersonalCenterConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class CmsPersonalCenterServiceImpl implements CmsPersonalCenterService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 查询CMS个人中心设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public CmsPersonalCenterConfigVo findCmsPersonalCenter(Long merId){
        CmsPersonalCenterConfigVo cmsPersonalCenterConfigVo = new CmsPersonalCenterConfigVo();
        //查询运营商CMS基础设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());
        if(merBaseConfig != null ){
            cmsPersonalCenterConfigVo.setCmsPersonalCenterConfig(merBaseConfig.getVal());
        }
        return cmsPersonalCenterConfigVo;
    }

    /**
     * 保存CMS个人中心设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsPersonalCenterConfigDto
     * @return null
     */
    public void updateCmsPersonalCenter(CmsPersonalCenterConfigDto cmsPersonalCenterConfigDto){

        if(StringUtils.isBlank(cmsPersonalCenterConfigDto.getCmsPersonalCenterConfig())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入CMS个人中心配置内容");
        }

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(cmsPersonalCenterConfigDto.getMerId(), MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(cmsPersonalCenterConfigDto.getCmsPersonalCenterConfig());
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(cmsPersonalCenterConfigDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());
            merBaseConfig.setVal(cmsPersonalCenterConfigDto.getCmsPersonalCenterConfig());
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //更新redis
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getKey(), cmsPersonalCenterConfigDto.getMerId()), merBaseConfig.getVal());
    }

    /**
     * 查询CMS官方默认的个人中心配置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public CmsPersonalCenterConfigVo findCmsPersonalCenterByDefault(){
        CmsPersonalCenterConfigVo cmsPersonalCenterConfigVo = new CmsPersonalCenterConfigVo();
        //先获取默认运营商id
        Long defaultMerId = uzaiConsoleSysConfig.getDefaultMerId();
        MerBaseConfig merBaseConfig_personal_center = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());
        if(merBaseConfig_personal_center != null ){
            cmsPersonalCenterConfigVo.setCmsPersonalCenterConfig(merBaseConfig_personal_center.getVal());
        }
        return cmsPersonalCenterConfigVo;
    }

}
